package com.yiren.dbaa.module.practice_set.map.view.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.currency.entity.MapEntity;
import com.yiren.dbaa.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author lwb
 * @date : 2022/11/25 10:18
 * @description: 地图首页适配器
 */

public class MapHomeListAdapter extends BaseAdapter {

    private Context context;
    private List<MapEntity> list;
    public int mSelect = 0;
    private boolean isPartner;//是否是合伙人

    public MapHomeListAdapter(Context context, List<MapEntity> list) {
        this.context = context;
        this.list = list;
        isPartner = LoadConfigApi.loadUserParamConfig(context).isPartner();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = View.inflate(context, R.layout.item_home_map, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        MapEntity map = list.get(position);
        viewHolder.tvMapName.setText(map.getName());
        //合伙人视角
        if (isPartner) {
            viewHolder.tvUpdateBtn.setVisibility(View.GONE);
            //可更新
            if (map.isUpdate()) {
                viewHolder.tvUpdate.setVisibility(View.VISIBLE);
            } else {
                viewHolder.tvUpdate.setVisibility(View.GONE);
            }
            if (map.getMapType() == 1) {
                viewHolder.tvMapType.setText("考试路段");
            } else {
                viewHolder.tvMapType.setText("训练路段");
            }
            viewHolder.tvBaseStation.setText("采集连接基站：" + map.getBaseName());
            //是否下载
            if (map.isDownLoad()) {
                viewHolder.tvManage.setText("管理");
            } else {
                viewHolder.tvManage.setText("下载");
            }
            //未发布的考规
            if (map.getMapVer() == 0) {
                viewHolder.tvMapVersion.setText("未公开发布，暂无版本");
                viewHolder.tvDelete.setVisibility(View.VISIBLE);
                viewHolder.tvUsingRange.setVisibility(View.GONE);
                viewHolder.tvManage.setText("管理");
                viewHolder.vBar.setVisibility(View.GONE);
            } else {
                viewHolder.vBar.setVisibility(View.VISIBLE);
                viewHolder.tvUsingRange.setVisibility(View.VISIBLE);
                viewHolder.tvMapVersion.setText("地图版本：V" + list.get(position).getMapVer());
                if (map.getUseRange() == 1) {
                    viewHolder.tvUsingRange.setText("使用范围：个别用户使用");
                } else {
                    viewHolder.tvUsingRange.setText("使用范围：全部用户使用");
                }
                viewHolder.tvDelete.setVisibility(View.GONE);
            }
        } else {  //教练视角
            viewHolder.tvUpdate.setVisibility(View.GONE);
            viewHolder.tvUsingRange.setVisibility(View.GONE);
            viewHolder.vBar.setVisibility(View.GONE);
            if (map.getMapType() == 1) {
                viewHolder.tvMapType.setText("考试路段");
            } else {
                viewHolder.tvMapType.setText("训练路段");
            }
            viewHolder.tvBaseStation.setText("采集连接基站：" + map.getBaseName());
            //是否下载
            if (map.isDownLoad()) {
                viewHolder.tvManage.setText("编辑地图");
            } else {
                viewHolder.tvManage.setText("下载");
            }
            //未发布
            if (map.getMapVer() == 0) {
                viewHolder.tvMapVersion.setText("未公开发布，暂无版本");
                viewHolder.tvDelete.setVisibility(View.VISIBLE);
                viewHolder.tvUsingRange.setVisibility(View.GONE);
                viewHolder.tvUpdateBtn.setVisibility(View.GONE);
                viewHolder.tvManage.setText("编辑地图");
            } else {
                viewHolder.tvMapVersion.setText("地图版本：V" + map.getMapVer());
                viewHolder.tvDelete.setVisibility(View.GONE);
                if (map.isUpdate()) {
                    viewHolder.tvUpdateBtn.setVisibility(View.VISIBLE);
                } else {
                    viewHolder.tvUpdateBtn.setVisibility(View.GONE);
                }
            }
        }
        viewHolder.tvUpdateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnUpdateClickListener != null) {
                    mOnUpdateClickListener.onUpdateClick(position);
                }
            }
        });
        viewHolder.tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnDeleteClickListener != null) {
                    mOnDeleteClickListener.onDeleteClick(position);
                }
            }
        });
        viewHolder.tvManage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnManageClickListener != null) {
                    mOnManageClickListener.onManageClick(position);
                }
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.tv_map_name)
        TextView tvMapName;
        @BindView(R.id.tv_update)
        TextView tvUpdate;
        @BindView(R.id.tv_map_version)
        TextView tvMapVersion;
        @BindView(R.id.tv_map_type)
        TextView tvMapType;
        @BindView(R.id.v_bar)
        View vBar;
        @BindView(R.id.tv_using_range)
        TextView tvUsingRange;
        @BindView(R.id.tv_base_station)
        TextView tvBaseStation;
        @BindView(R.id.tv_update_btn)
        TextView tvUpdateBtn;
        @BindView(R.id.tv_delete)
        TextView tvDelete;
        @BindView(R.id.tv_manage)
        TextView tvManage;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    //更新点击回调
    public interface OnUpdateClickListener {
        void onUpdateClick(int position);
    }

    private OnUpdateClickListener mOnUpdateClickListener;

    public void setOnUpdateClickListener(OnUpdateClickListener onUpdateClickListener) {
        mOnUpdateClickListener = onUpdateClickListener;
    }

    //条目删除回调
    public interface OnDeleteClickListener {
        void onDeleteClick(int position);
    }

    private OnDeleteClickListener mOnDeleteClickListener;

    public void setOnDeleteClickListener(OnDeleteClickListener onDeleteClickListener) {
        mOnDeleteClickListener = onDeleteClickListener;
    }

    //管理点击回调
    public interface OnManageClickListener {
        void onManageClick(int position);
    }

    private OnManageClickListener mOnManageClickListener;

    public void setOnManageClickListener(OnManageClickListener onManageClickListener) {
        mOnManageClickListener = onManageClickListener;
    }

}